import 'package:flutter/material.dart';// 导入 Material UI 组件库
import 'package:manual/app.dart';
import 'package:manual/config/app_config.dart';
import 'package:manual/initialization/init_app.dart';
import 'package:manual/providers/theme_provider.dart';
import 'package:flutter/rendering.dart';// 导入调试工具
import 'package:manual/utils/startup_monitor.dart';
import 'package:provider/provider.dart'; // providers
import 'package:manual/providers/image_list_provider.dart'; // 图片列表

// 应用入口 main() 方法
void main() async {
  final startupMonitor = StartupMonitor();
  startupMonitor.mark("mainStart");

  WidgetsFlutterBinding.ensureInitialized(); //  确保 Flutter 框架初始化 再进行初始化操作
  startupMonitor.mark("Flutter-Initialized");
  

  // 加载应用配置
  const appConfigRepository = AppConfigRepository();
  // ignore: unused_local_variable
  final appConfig = await appConfigRepository.load();
  startupMonitor.mark("AppConfig-Initialized");


  // 初始化应用
  await initApp(); 
  startupMonitor.mark("initApp-Initialized");
  debugPaintSizeEnabled = false; // 开启调试模式
  // 创建 ThemeProvider 实例
  final themeProvider = ThemeProvider();
  await themeProvider.initializeTheme(); // 初始化主题

  startupMonitor.mark("theme-Initialized");
  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (_) => ImageListProvider()),
      ChangeNotifierProvider(create: (_) => themeProvider), // 添加 ThemeProvider
    ],
    child: MyApp(config: appConfig),
  ));

  WidgetsBinding.instance.addPostFrameCallback((_) {
    startupMonitor.mark("firstFrameRendered");
    startupMonitor.logResults();
  });
}
